package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContext;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.entity.BtyCwHuopei;
import cn.stylefeng.guns.sys.modular.system.entity.BtyCwJs;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyCwHuopeiMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyCwJsMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyCwJsParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyCwJsResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyCwHuopeiService;
import cn.stylefeng.guns.sys.modular.system.service.BtyCwJsService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 货赔酒水单价表 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-11-22
 */
@Service
public class BtyCwJsServiceImpl extends ServiceImpl<BtyCwJsMapper, BtyCwJs> implements BtyCwJsService {
    @Autowired
    private BtyCwHuopeiMapper cwHuopeiMapper;
    @Autowired
    private BtyCwHuopeiService cwHuopeiService;

    @Override
    public ResponseData add(BtyCwJsParam param){
        boolean save = false;

        BtyCwJs entity = getEntity(param);
        String month = DateUtil.getLastMonthStr();//获取当前月的前一月

        entity.setDate(month);
        entity.setCreateUser(LoginContextHolder.getContext().getUser().getId());
        //获取数据库是否存在此数据
        int con = baseMapper.count(entity);
        if (con == 0){
            //数据库新增该酒水单价
            save = this.save(entity);
        }else {
            return ResponseData.error("已存在该品项！");
        }

        if (save && ToolUtil.isNotEmpty(param.getPrice())){
            //同步货赔表酒水卖价(酒水业务日期 与 货赔表业务日期截止日期 相等)
            BtyCwHuopei cwHuopei = new BtyCwHuopei();
            cwHuopei.setPrice(param.getPrice().toString());
            cwHuopei.setProCode(param.getProCode());
            cwHuopeiMapper.editPrice(cwHuopei,month);

            //获取修改卖价的主键ID
            List<BtyCwHuopei> list = cwHuopeiMapper.getIdList(param.getProCode(),month);
            for (BtyCwHuopei huopei : list) {
                BigDecimal price = new BigDecimal(huopei.getPrice());
                BigDecimal num = new BigDecimal(huopei.getNumTotal());

                BigDecimal amountTotal = price.multiply(num).setScale(2, BigDecimal.ROUND_HALF_UP);
                huopei.setAmountTotal(amountTotal.toString());
            }
            if (ToolUtil.isNotEmpty(list)){
                cwHuopeiMapper.editAmount(list);
                cwHuopeiService.editHpPankui(list);//计算盘亏数据
            }

        }
        return ResponseData.success();
    }

    @Override
    public void delete(BtyCwJsParam param){
        this.removeById(getKey(param));
    }

    @Override
    public ResponseData update(BtyCwJsParam param){
        BtyCwJs oldEntity = getOldEntity(param);
        BtyCwJs newEntity = getEntity(param);
        newEntity.setUpdateUser(LoginContextHolder.getContext().getUser().getId());
        ToolUtil.copyProperties(newEntity, oldEntity);
        //数据库修改该酒水单价
        boolean b = this.updateById(newEntity);

        if (b){
            //查询该店业务日期
            BtyCwJs btyCwJs = baseMapper.selectById(param.getPxId());
            String month = btyCwJs.getDate();

            //同步货赔表酒水卖价(酒水业务日期 与 货赔表业务日期截止日期 相等)
            BtyCwHuopei cwHuopei = new BtyCwHuopei();
            cwHuopei.setPrice(param.getPrice().toString());
            cwHuopei.setProCode(param.getProCode());
            cwHuopeiMapper.editPrice(cwHuopei,month);

            //获取修改卖价的主键ID
            List<BtyCwHuopei> list = cwHuopeiMapper.getIdList(param.getProCode(),month);
            for (BtyCwHuopei huopei : list) {
                BigDecimal price = new BigDecimal(huopei.getPrice());
                BigDecimal num = new BigDecimal(huopei.getNumTotal());

                BigDecimal amountTotal = price.multiply(num).setScale(2, BigDecimal.ROUND_HALF_UP);
                huopei.setAmountTotal(amountTotal.toString());
            }

            if (ToolUtil.isNotEmpty(list)){
                cwHuopeiMapper.editAmount(list);

                cwHuopeiService.editHpPankui(list);//计算盘亏数据
            }
        }
        return ResponseData.success();
    }

    @Override
    public BtyCwJsResult findBySpec(BtyCwJsParam param){
        return null;
    }

    @Override
    public List<BtyCwJsResult> findListBySpec(BtyCwJsParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyCwJsParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 新增冻货接口
     * @param param
     * @return
     */
    @Override
    public ResponseData addDh(BtyCwJsParam param) {
        boolean save = false;

        BtyCwJs entity = getEntity(param);
        String month = DateUtil.getLastMonthStr();//获取当前月的前一月

        entity.setDate(month);
        entity.setCreateUser(LoginContextHolder.getContext().getUser().getId());
        //获取数据库是否存在此数据
        int con = baseMapper.countDh(entity);
        if (con == 0){
            //数据库新增该冻货/酱料单价
            save = this.save(entity);
        }else {
            return ResponseData.error("已存在该品项类别！");
        }

        if (save && ToolUtil.isNotEmpty(param.getPrice())){
            String className = "";
            //同步货赔表冻货/酱料卖价(酒水业务日期 与 货赔表业务日期截止日期 相等)
            BtyCwHuopei cwHuopei = new BtyCwHuopei();
            cwHuopei.setPrice(param.getPrice().toString());
            if (param.getClassName().equals("2")){
                className = "酱料";
            }else if (param.getClassName().equals("3")){
                className = "冻货";
            }
            cwHuopei.setClassName(className);
            cwHuopei.setScmClassCode(param.getScmClassCode());
            cwHuopeiMapper.editDhPrice(cwHuopei,month);

            //获取修改卖价的主键ID
            List<BtyCwHuopei> list = cwHuopeiMapper.getIdsList(cwHuopei.getClassName(),month);
            if (ToolUtil.isNotEmpty(list)){
                for (BtyCwHuopei huopei : list) {
                    BigDecimal price = new BigDecimal(huopei.getPrice());
                    BigDecimal num = new BigDecimal(huopei.getNumTotal());

                    //耗用金额分差 = 非税成本单价 * 耗用数量分差
                    BigDecimal amountTotal = price.multiply(num).setScale(2, BigDecimal.ROUND_HALF_UP);
                    huopei.setAmountTotal(amountTotal.toString());
                }

                //每500条保存一次
                for(int i = 0; i < list.size();){
                    int i2 = (i + 500);
                    i2 = i2 > list.size() ? list.size() : i2;
                    List<BtyCwHuopei> sub = list.subList(i, i2);
                    //修改耗用金额分差
                    cwHuopeiMapper.editAmount(sub);
                    i = i2;
                }
            }

            cwHuopeiService.editHpPankui(list);//计算盘亏数据

        }
        return ResponseData.success();
    }

    /**
     * 编辑冻货与酱料单价接口
     * @param param
     * @return
     */
    @Override
    public ResponseData updateDh(BtyCwJsParam param) {
        BtyCwJs oldEntity = getOldEntity(param);
        BtyCwJs newEntity = getEntity(param);
        newEntity.setUpdateUser(LoginContextHolder.getContext().getUser().getId());
        ToolUtil.copyProperties(newEntity, oldEntity);
        //数据库修改该酒水单价
        boolean b = this.updateById(newEntity);

        if (b){
            String className = "";
            //查询该店业务日期
            BtyCwJs btyCwJs = baseMapper.selectById(param.getPxId());
            String month = btyCwJs.getDate();

            //同步货赔表冻货，酱料卖价(业务日期 与 货赔表业务日期截止日期 相等)
            BtyCwHuopei cwHuopei = new BtyCwHuopei();
            cwHuopei.setPrice(param.getPrice().toString());
            if (param.getClassName().equals("2")){
                className = "酱料";
            }else if (param.getClassName().equals("3")){
                className = "冻货";
            }
            cwHuopei.setClassName(className);
            cwHuopei.setScmClassCode(param.getScmClassCode());
            cwHuopeiMapper.editDhPrice(cwHuopei,month);

            //获取修改卖价的主键ID
            List<BtyCwHuopei> list = cwHuopeiMapper.getIdsList(cwHuopei.getClassName(),month);
            if (ToolUtil.isNotEmpty(list)){
                for (BtyCwHuopei huopei : list) {
                    BigDecimal price = new BigDecimal(huopei.getPrice());
                    BigDecimal num = new BigDecimal(huopei.getNumTotal());

                    //耗用金额分差 = 非税成本单价 * 耗用数量分差
                    BigDecimal amountTotal = price.multiply(num).setScale(2, BigDecimal.ROUND_HALF_UP);
                    huopei.setAmountTotal(amountTotal.toString());
                }

                //每500条保存一次
                for(int i = 0; i < list.size();){
                    int i2 = (i + 500);
                    i2 = i2 > list.size() ? list.size() : i2;
                    List<BtyCwHuopei> sub = list.subList(i, i2);
                    //修改耗用金额分差
                    cwHuopeiMapper.editAmount(sub);
                    i = i2;
                }
            }

            cwHuopeiService.editHpPankui(list);//计算盘亏数据
        }
        return ResponseData.success();
    }

    private Serializable getKey(BtyCwJsParam param){
        return param.getPxId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyCwJs getOldEntity(BtyCwJsParam param) {
        return this.getById(getKey(param));
    }

    private BtyCwJs getEntity(BtyCwJsParam param) {
        BtyCwJs entity = new BtyCwJs();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
